package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.StudentNews;
import com.xmy.cultivate.mapper.StudentNewsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.ICourseSchedulingDetailService;
import com.xmy.cultivate.service.IStudentNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 新生名单（连续两期没有报名之后再报名的学生），做新生缺课使用 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-11-22
 */
@Service
public class StudentNewsServiceImpl extends ServiceImpl<StudentNewsMapper, StudentNews> implements IStudentNewsService {

    @Autowired
    StudentNewsMapper studentNewsMapper;

    @Autowired
    ICourseSchedulingDetailService iCourseSchedulingDetailService;


    @Override
    public boolean addStudentNews(StudentNews studentNews) {
        // 判断是否已经添加过
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentNews.getStudentId());
        wrapper.eq("subjects_id", studentNews.getSubjectsId());
        wrapper.eq("year_part", studentNews.getYearPart());
        wrapper.eq("quarter_num", studentNews.getQuarterNum());
        Long count = studentNewsMapper.selectCount(wrapper);

        if (count > 0) {
            return false;
        }

        // 添加到数据库
        studentNews.setAddDate(LocalDate.now());
        studentNewsMapper.insert(studentNews);

        return true;
    }

    /**
     *
     * @param yearPart
     * @param quarterNum
     * @param studentId
     * @param subjectsId
     * @return
     */
    @Override
    public StudentNews getDataOne(Integer yearPart, Integer quarterNum,Long studentId,Long subjectsId){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        wrapper.eq("subjects_id",subjectsId);
        wrapper.eq("year_part", yearPart);
        wrapper.eq("quarter_num", quarterNum);
        wrapper.last("limit 1");
        StudentNews studentNews = studentNewsMapper.selectOne(wrapper);
        return studentNews;
    }
}
